﻿CREATE PROCEDURE [dbo].[Insurer_InsertUpdate]

	@id					BIGINT,

	@entityId			BIGINT,

	@insurerType			INT, 

	@nationalPlanId		VARCHAR (0020),

	@modifiedId			VARCHAR (0060)
	
AS

	BEGIN 

		MERGE Insurer

			USING (SELECT @id) AS Source (Id)

				ON Insurer.Id = Source.Id

			WHEN MATCHED THEN 

				UPDATE SET 

					NationalPlanId = ISNULL (@nationalPlanId, ''),

					ModifiedId = @modifiedId,

					ModifiedDateTime = GETDATE ()

			WHEN NOT MATCHED THEN 

				INSERT (EntityId, InsurerType, NationalPlanId, CreateId, CreateDateTime, ModifiedId, ModifiedDateTime) 

					VALUES (@entityId, @insurerType, ISNULL (@nationalPlanId, ''), @modifiedId, GETDATE (), @modifiedId, GETDATE())

		; -- MERGE ( END )
		
	END

	